package io.gbznt.modules.buss.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.R;
import io.gbznt.common.utils.ShiroUtils;
import io.gbznt.common.validator.ValidatorUtils;
import io.gbznt.common.validator.group.AddGroup;
import io.gbznt.modules.buss.entity.BussCameraInfoEntity;
import io.gbznt.modules.buss.entity.BussFarmlandInfoEntity;
import io.gbznt.modules.buss.service.BussCameraInfoService;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;

/**
 * 摄像头信息
 *
 * @author gbznt
 * @email http://www.gbznt.com/
 * @date 2019-04-20 15:48:34
 */
@RestController
@RequestMapping("buss/busscamerainfo")
public class BussCameraInfoController {
	@Autowired
	private BussCameraInfoService bussCameraInfoService;
	@Autowired
	private BussFarmlandInfoService bussFarmlandInfoService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	// @RequiresPermissions("buss:busscamerainfo:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = bussCameraInfoService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	// @RequiresPermissions("buss:busscamerainfo:info")
	public R info(@PathVariable("id") String id) {
		BussCameraInfoEntity bussCameraInfo = bussCameraInfoService.getById(id);

		return R.ok().put("bussCameraInfo", bussCameraInfo);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	// @RequiresPermissions("buss:busscamerainfo:save")
	public R save(@RequestBody BussCameraInfoEntity bussCameraInfo) {
		ValidatorUtils.validateEntity(bussCameraInfo, AddGroup.class);

		bussCameraInfo.setCreateId(ShiroUtils.getUserId() + "");
		bussCameraInfo.setCreateTime(new Date());
		bussCameraInfo.setCreateName(ShiroUtils.getUserName());
		if (StringUtils.isNotBlank(bussCameraInfo.getFarmlandInfoId())) {
			BussFarmlandInfoEntity bussFarmlandInfo = bussFarmlandInfoService
					.getById(bussCameraInfo.getFarmlandInfoId());
			if (bussFarmlandInfo != null && StringUtils.isNotBlank(bussFarmlandInfo.getDeptId())) {
				bussCameraInfo.setDeptId(bussFarmlandInfo.getDeptId());
			}
		}
		bussCameraInfoService.save(bussCameraInfo);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	// @RequiresPermissions("buss:busscamerainfo:update")
	public R update(@RequestBody BussCameraInfoEntity bussCameraInfo) {
		ValidatorUtils.validateEntity(bussCameraInfo, AddGroup.class);

		bussCameraInfo.setUpdateId(ShiroUtils.getUserId() + "");
		bussCameraInfo.setUpdateTime(new Date());
		bussCameraInfo.setUpdateName(ShiroUtils.getUserName());
		if (StringUtils.isNotBlank(bussCameraInfo.getFarmlandInfoId())) {
			BussFarmlandInfoEntity bussFarmlandInfo = bussFarmlandInfoService
					.getById(bussCameraInfo.getFarmlandInfoId());
			if (bussFarmlandInfo != null && StringUtils.isNotBlank(bussFarmlandInfo.getDeptId())) {
				bussCameraInfo.setDeptId(bussFarmlandInfo.getDeptId());
			}
		}
		Boolean re = bussCameraInfoService.updateById(bussCameraInfo);// 全部更新

		return re ? R.ok() : R.oldVersion();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	// @RequiresPermissions("buss:busscamerainfo:delete")
	public R delete(@RequestBody String[] ids) {
		bussCameraInfoService.removeByIds(Arrays.asList(ids));

		return R.ok();
	}

	/**
	 * 获取所有的摄像头信息
	 */
	@RequestMapping("/getCameraAll")
	public R getCameraAll(@RequestParam Map<String, Object> params) {
		PageUtils page = bussCameraInfoService.queryPage(params);

		return R.ok().put("page", page);
	}

}
